package hx.com.utils;

import hx.com.entity.*;

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class CardUtils implements Serializable {
    private static final long serialVersionUID = 472203812839714345L;
    private Map<String,MobileCard> cards=new TreeMap<>();
    private Map<String, List<ConsumInfo>> consumInfos;
    private List<ConsumInfo>list=new ArrayList<>();

    public void showCards(){
        System.out.println(cards);
    }

    public void addCard(String s,MobileCard m){
        cards.put(s, m);
    }
    //判断输入的号码是否存在（求助后得出）
    public boolean isExists(String number,String passWord){
        MobileCard card=cards.get(number);
        if(card!=null&&card.getPassWord().equals(passWord))
            return true;
        return false;
    }

    public void payCheek(String number){
        MobileCard card=cards.get(number);//电话卡具体信息在card里
        ServicePackage servicePackage=card.getScrPakage();

        if(servicePackage instanceof NetPackage){
            NetPackage netPackage= (NetPackage) servicePackage;//获取对象类型
            NetPackage net=new NetPackage();

            int useflow=card.getRealFlow()-net.getFlow();
            if(useflow>=0){
            double sum=useflow*0.1+net.getPrice();
            double remain=card.getMoney()-sum;

            System.out.println("套餐资费："+net.getPrice());
            System.out.println("总计："+sum);
            System.out.println("余额："+remain);}

            else {
                System.out.println("套餐资费："+net.getPrice());
                System.out.println("总计："+net.getPrice());
                System.out.println("余额："+(card.getMoney()-net.getPrice()));}

        }
        else if(servicePackage instanceof TalkPackage){
            TalkPackage talkPackage= (TalkPackage) servicePackage;

            TalkPackage talk=new TalkPackage();

            int usetalk=card.getRealTalkTime()-talk.getTalkTime();
            int usecount=card.getRealSMSCCount()-talk.getSumsCount();
            if(usetalk>=0||usecount>=0){
                double sum=usetalk*0.1+usecount*0.1+talk.getPrice();
                double remain=card.getMoney()-sum;
                System.out.println("套餐资费："+talk.getPrice());
                System.out.println("总计："+sum);
                System.out.println("余额："+remain);
            }
            else {
                System.out.println("套餐资费："+talk.getPrice());
                System.out.println("总计："+talk.getPrice());
                System.out.println("余额："+(card.getMoney()-talk.getPrice()));
            }
        }
        else if(servicePackage instanceof SuperPackage){
            SuperPackage superPackage= (SuperPackage) servicePackage;
            SuperPackage sup=new SuperPackage();

            int usetalk=card.getRealTalkTime()-sup.getTalkTime();
            int usemes=card.getRealSMSCCount()-sup.getSmsCount();
            int useflow=card.getRealFlow()-sup.getFlow();

            if(usetalk>=0||usemes>=0||useflow>=0){
                double sum=usetalk*0.1+usemes*0.1+useflow*0.1+card.getScrPakage().getPrice();
                double remain=card.getMoney()-sum;
                System.out.println("套餐资费："+sup.getPrice());
                System.out.println("总计："+sum);
                System.out.println("余额："+remain);
            }else {
                System.out.println("套餐资费："+sup.getPrice());
                System.out.println("总计："+sup.getPrice());
                System.out.println("余额："+(card.getMoney()-sup.getPrice()));
            }

        }

    }

    public void printConsumInfo(String number){
        try { consumInfos=getConsumInfos();

            OutputStreamWriter opw=new OutputStreamWriter(new FileOutputStream("\\mobile-service\\"+number+".txt"),"UTF-8");
            if (consumInfos.get(number)!=null){

                String data="====="+number+"消费纪录====="+"\r\n"+"序号：\t"+"类型：\t"+"数据（通话（条）/上网（Mb）/短信（条））"+"\r";
                int s=1;
                for(ConsumInfo info:consumInfos.get(number)){
                    String data2="\n\r"+String.valueOf(s)+".\t"+info.getType()+"\t"+info.getConsumData();
                    data=data+data2;
                    s++;
                }
                int cc;
                opw.write(data);
                opw.close();

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void deleteCard(String number){
        cards.remove(number);
    }

    public  void saveCards(){
        FileOutputStream fileOutputStream= null;
        try {
            fileOutputStream = new FileOutputStream("e:\\myjava\\moblie-service\\cardsinformation.dat");
            ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(cards);
            objectOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Map<String, MobileCard> getCards() {
        try {
            FileInputStream readCards=new FileInputStream("e:\\myjava\\moblie-service\\cardsinformation.dat");
            ObjectInputStream objectInputStream=new ObjectInputStream(readCards);
            cards=(Map<String, MobileCard>)objectInputStream.readObject();
            objectInputStream.close();
            System.out.println("read over");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cards;
    }

    public Map<String, List<ConsumInfo>> getConsumInfos() {
        try {
            FileInputStream savaConsumInfo=new FileInputStream("e:\\myjava\\moblie-service\\consumInformation.dat");
            ObjectInputStream objectInputStream=new ObjectInputStream(savaConsumInfo);
            consumInfos=(Map<String, List<ConsumInfo>>) objectInputStream.readObject();
            objectInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return consumInfos;
    }

    public void saveconsumInofs(){
        try {
            FileOutputStream fileOutputStream=new FileOutputStream("e:\\myjava\\moblie-service\\cardsinformation.dat");
            ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(consumInfos);
            objectOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void addConsunInfo(String number,ConsumInfo info){
        list.add(info);
        consumInfos.put(number,list);
    }

    public void showRemainDetail(String number){
        cards.get(number).getScrPakage().showInfo();
    }

    public void setConsumInfos(Map<String, List<ConsumInfo>> consumInfos) {
        this.consumInfos = consumInfos;
    }

    public static String createCardNumber() {
        Random random=new Random();
            String start="139";
            for(int j=0;j<8;j++){
                String endNumbers=String.valueOf(random.nextInt(9));
                start=start+endNumbers;
            }
            return  start;
    }


}
